/**
 * 加载glb格式模型
 * @param {Earth} options.earth 渲染容器
 * @param {Array} options.position 模型中心坐标位置 [ lng, lat, h ]
 * @param {Array} options.xbsjRotation 模型笛卡尔坐标下旋转角度 [偏航角、俯仰角、翻转角]
 * @param {String} options.url 模型文件路径
 * @param {Number} options.scale 模型缩放倍率
 * @param {object} options.data 临时数据存放
 * 
 * onReady 加载结束触发
 * 
 * @returns {LoadModel} LoadModel实例
 */


function LoadModel (options) {
    let radian = Math.PI / 180;
    let angle =  180 / Math.PI;
    this.timer = null
    
    let model = new XE.Obj.Model(options.earth);


    let config = {
        url: options.url,
        // minimumPixelSize: 128,
        // maximumScale: 1,
        scale: options.scale || 1,
        xbsjPosition: [
            options.position[0] * radian,
            options.position[1] * radian,
            options.position[2] || 0,
        ],
        xbsjRotation: options.xbsjRotation ? [
            angle * options.xbsjRotation[0],
            angle * options.xbsjRotation[1],
            angle * options.xbsjRotation[2]
        ] : [ 0, 0, 0 ]
    }

    model.xbsjFromJSON(config)
    let cesiumModel = model.nativeModel()
    this.timer = setInterval(() => {
        if (cesiumModel.ready) {
            clearInterval(this.timer)
            this.timer = null
            if (this.onReady) {
                this.onReady()
            }
        }
    }, 100)

    model._node = this
    this.data = options.data || null
    this.XEObj = model
}


LoadModel.prototype.setPositions = function (options) {
    let radian = Math.PI / 180;
    this.XEObj.xbsjPosition = [ 
        options[0] * radian,
        options[1] * radian, 
        options[2] || 0,
    ]
}

LoadModel.prototype.click = function () {
    if (this.onclick) { this.onclick() }
}

LoadModel.prototype.destroy = function () {
    this.XEObj._node = null
    if (this.timer) {
        clearInterval(this.timer)
        this.timer = null
    }
    this.XEObj.destroy()
}


export default LoadModel